package com.earnings.okhttputils.utils.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import java.util.List;


/**
 * Created by yy on 2015/5/10.
 */
public abstract class DataBindingBaseadapter<T,F > extends RecyclerView.Adapter<DataBindingViewHolder>  {
    protected Context mContext;
    protected List<T> mDatas;
    protected F binding;
    protected LayoutInflater mInflater;
    private int mLayoutItem;
    private List<Integer> mLayoutItemList;
    private boolean isOpenAnimation=false;
    private CustomAnimation customAnimation;
    private boolean isFirstOnly = true;
    //descendantFocusability 用于解决item中的控件抢获事件
    //bean的用处在于 如果是单选和多选等时候可以使用来保存选中状态

    /**
     * @param context    上下文
     * @param datas      数据源
     * @param layoutItem 布局id
     */
    public DataBindingBaseadapter(Context context, List<T> datas, int layoutItem) {
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
        mLayoutItem = layoutItem;
        customAnimation = new CustomAnimation();
    }

    public DataBindingBaseadapter(Context context, List<T> datas, List<Integer> layoutItem) {
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
        mLayoutItemList = layoutItem;
        customAnimation = new CustomAnimation();
    }

    public List<T> getmDatas() {

        return mDatas;
    }

    public void setmDatas(List<T> Datas) {
        mDatas.clear();
        mDatas = Datas;
        notifyDataSetChanged();
    }

    public void setMoreDatas(List<T> Datas) {

        for (int i = 0; i < Datas.size(); i++) {
            mDatas.add(Datas.get(i));
        }

        notifyDataSetChanged();
    }




    //    @Override
//    public int getCount() {
//        return mDatas.size();
//    }
//
//    @Override
//    public T getItem(int position) {
//        return mDatas.get(position);
//    }
//
//    @Override
//    public long getItemId(int position) {
//        return position;
//    }
//
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//
//        ViewHolder holder = ViewHolder.get(mContext, convertView, parent, mLayoutItem, position, getCount());
//        convert(holder, getItem(position));
//        return holder.getConvertView();
//    }

    private int mLastPosition = -1;
    @Override
    public void onBindViewHolder(DataBindingViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);

    }
    public void setFirstOnly(boolean firstOnly) {
        isFirstOnly = firstOnly;

    }

    public void setOpenAnimation(boolean OpenAnimation) {
        isOpenAnimation = OpenAnimation;

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onBindViewHolder(final DataBindingViewHolder holder, final int position) {
        if (isOpenAnimation) {
            if (!isFirstOnly || position > mLastPosition) {
                BaseAnimation animation = null;
                if (customAnimation != null) {
                    animation = customAnimation;
                }else{
                    animation = customAnimation;
                }
                for (Animator anim : animation.getAnimators(holder.itemView)) {
                    anim.setDuration(300).start();
                    anim.setInterpolator(new LinearInterpolator());
                }
                mLastPosition = position;
            }
        }
        getItemViewType(position);
        convert(holder,(F) holder.getBinding(),mDatas.get(position));
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(holder,mDatas.get(position));
            }
        });
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
           // holder.itemView.setBackground(ContextCompat.getDrawable(holder.itemView.getContext(), R.drawable.top_bar_button));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(mDatas.get(position), holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(mDatas.get(position), holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(Object obj, View view, int position);

        void onItemLongClick(Object obj, View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public DataBindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return DataBindingViewHolder.get(mContext,  DataBindingUtil.inflate(mInflater,
                    mLayoutItem, parent, false), viewType);
    }

    public void removeItem(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
//        if (position != mDatas.size()) {      // 这个判断的意义就是如果移除的是最后一个，就不用管它了，= =whatever，老板还不发工资啊啊啊啊啊啊
        notifyItemRangeChanged(position, mDatas.size());
//        }
    }

    public abstract void convert(DataBindingViewHolder holder, F binding, T t);
    public abstract void onItemClick(DataBindingViewHolder holder,T t);

    public interface  BaseAnimation {

        Animator[] getAnimators(View view);

    }

    /**
     * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
     */
    public class CustomAnimation implements BaseAnimation {

        @Override
        public Animator[] getAnimators(View view) {
            return new Animator[]{
                    ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                    ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
            };
        }
    }
}
